package minishell.cmdlet.buildin;

import java.lang.reflect.AccessibleObject;
import java.util.ArrayList;
import java.util.List;

import org.lex.io.IO;

import minishell.cmdlet.AbstractCmdlet;
import minishell.object.IObject;
import minishell.object.ListUtilities;
import minishell.object.StringObject;

public class SpyCmdlet extends AbstractCmdlet
{
	private List<IObject> input = null;
	private List<IObject> result = null;

	public SpyCmdlet( List<IObject> input )
	{
		this.input = input;
		this.result = new ArrayList<IObject>();
	}

	public List<IObject> excute( String arg )
	{
		if( null == input || 0 == input.size() )
			return ListUtilities.emptyList();
		IObject object = input.get( 0 );
		//=====================================
		this.addString( "Properties:" );
		String[] properties = object.properties();
		for( String p : properties )
			this.addString( p );
		//=====================================
		Object primitive = object.getPrimitive();
		if( null == primitive )
			return this.result;
		//=====================================
		Class c = primitive.getClass();
		this.addNewLine();
		this.addString( "Type: " + c.getCanonicalName() );
		this.addNewLine();
		this.addString( "Constructors:" );
		this.addAccessibleObject( c.getConstructors() );
		this.addNewLine();
		this.addString( "Fields:" );
		this.addAccessibleObject( c.getFields() );
		this.addNewLine();
		this.addString( "Methods:" );
		this.addAccessibleObject( c.getMethods() );
		return result;
	}
	
	private void addAccessibleObject( AccessibleObject[] array )
	{
		for( AccessibleObject a : array )
			this.addString( a.toString() );
	}

	private void addString( String string )
	{
		this.result.add( new StringObject( string ) );
	}
	
	private void addNewLine()
	{
		this.addString( IO.newline );
	}
}
